**Code produced in 2015-03-13 by Allan Dafoe and Devin Caughey

clear 

** Set base working directory
cd "/Users/Allan/Dropbox/1Papers/1Reputation/Honor-Culture-War/2012-05/Replication-Final/"

log using "2-150317-Data-Merge/2-150317-data-merge-log.smcl", replace

**** Load MID 4.01B data, downloaded from 
**** http://www.correlatesofwar.org/data-sets/MIDs
import delimited "1-Original-Data/MIDB_4.01-2.csv"

** Drop MIDs not involving the US
keep if ccode==2

sort dispnum3 dispnum4 ccode
save "1-Original-Data/MIDB-US.dta", replace


**** Load MID 4.01A data
clear
import delimited "1-Original-Data/MID-level/MIDA_4.01.csv"
sort dispnum3 dispnum4
save "1-Original-Data/MIDA.dta", replace


**** Merge MIDB and MIDA data
clear
use "1-Original-Data/MIDB-US.dta"
merge 1:1 dispnum3 dispnum4 using "1-Original-Data/MIDA.dta", gen(mergeid)

keep if mergeid==3
drop mergeid

** Generating start and end dates
gen StartDateUS = mdy(stmon, stday, styear) 
gen EndDateUS = mdy(endmon, endday, endyear)

** Some missing values due to missing stday or endday. 
** If MID started or ended in the same month, then impute missing day to the same day.
replace StartDateUS = mdy(stmon, endday, styear) if StartDateUS==. & stmon==endmon & styear==endyear
replace EndDateUS = mdy(endmon, stday, endyear) if EndDateUS==. & stmon==endmon & styear==endyear

** Impute missing days with the 15th (an approximation)
replace StartDateUS = mdy(stmon, 15, styear) if StartDateUS==.  
replace EndDateUS = mdy(endmon, 15, endyear) if EndDateUS==.

format StartDateUS EndDateUS %td 
order StartDateUS EndDateUS

gen LngthMIDUS = EndDateUS - StartDateUS + 1


**** Assigning Outcome
rename outcome outcomeMID
gen outcome = .

** outcome = 1 if US has Victory or other side Yields
replace outcome = 1 if sidea==1 & (outcomeMID==1 | outcomeMID==4)
replace outcome = 1 if sidea==0 & (outcomeMID==2 | outcomeMID==3)

** outcome = -1 if other has Victory or US Yields
replace outcome = -1 if sidea==1 & (outcomeMID==2 | outcomeMID==3)
replace outcome = -1 if sidea==0 & (outcomeMID==1 | outcomeMID==4)

** outcome = 0 if other coding
replace outcome = 0 if outcomeMID>4 | outcomeMID==. | outcomeMID < 0

** Indicator for whether the US used force in the MID
gen ForceUS = 0
replace ForceUS = 1 if hostlev >= 4      

** Ordering Variables: date, outcome variables, treatment, controls
order StartDateUS EndDateUS outcome fatality LngthMIDUS ForceUS


**** Generate President variables
gen Pres1 = ""
order Pres1 EndDateUS StartDateUS

sort StartDateUS dispnum3 dispnum4

** Assign presidents to MIDs
replace Pres1 = "Madison" if StartDateUS>=mdy(03, 04, 1809) & EndDateUS<mdy(03, 04, 1817)
replace Pres1 = "Monroe"  if StartDateUS>=mdy(03, 04, 1817) & EndDateUS<mdy(03, 04, 1825)
replace Pres1 = "AdamsJQ" if StartDateUS>=mdy(03, 04, 1825) & EndDateUS<mdy(03, 04, 1829)
replace Pres1 = "Jackson" if StartDateUS>=mdy(03, 04, 1829) & EndDateUS<mdy(03, 04, 1837)
replace Pres1 = "Van_Buren" if StartDateUS>=mdy(03, 04, 1837) & EndDateUS<mdy(03, 04, 1841)
replace Pres1 = "HarrisonWH" if StartDateUS>=mdy(03, 04, 1841) & EndDateUS<mdy(04, 04, 1841)
replace Pres1 = "Tyler" if StartDateUS>=mdy(04, 04, 1841) & EndDateUS<mdy(03, 04, 1845)
replace Pres1 = "Polk" if StartDateUS>=mdy(03, 04, 1845) & EndDateUS<mdy(03, 04, 1849)
replace Pres1 = "Taylor" if StartDateUS>=mdy(03, 04, 1849) & EndDateUS<mdy(03, 09, 1850)
replace Pres1 = "Fillmore" if StartDateUS>=mdy(03, 09, 1850) & EndDateUS<mdy(03, 04, 1853)
replace Pres1 = "Pierce" if StartDateUS>=mdy(03, 04, 1853) & EndDateUS<mdy(03, 04, 1857)
replace Pres1 = "Buchanan" if StartDateUS>=mdy(03, 04, 1857) & EndDateUS<mdy(03, 04, 1861)
replace Pres1 = "Lincoln" if StartDateUS>=mdy(03, 04, 1861) & EndDateUS<mdy(04, 15, 1865)
replace Pres1 = "JohnsonA" if StartDateUS>=mdy(04, 15, 1865) & EndDateUS<mdy(03, 04, 1869)
replace Pres1 = "Grant" if StartDateUS>=mdy(03, 04, 1869) & EndDateUS<mdy(03, 04, 1877)
replace Pres1 = "Hayes" if StartDateUS>=mdy(03, 04, 1877) & EndDateUS<mdy(03, 04, 1881)
replace Pres1 = "Garfield" if StartDateUS>=mdy(03, 04, 1881) & EndDateUS<mdy(09, 19, 1881)
replace Pres1 = "Arthur" if StartDateUS>=mdy(09, 19, 1881) & EndDateUS<mdy(03, 04, 1885)
replace Pres1 = "Cleveland1" if StartDateUS>=mdy(03, 04, 1885) & EndDateUS<mdy(03, 04, 1889)
replace Pres1 = "HarrisonB" if StartDateUS>=mdy(03, 04, 1889) & EndDateUS<mdy(03, 04, 1893)
replace Pres1 = "Cleveland2" if StartDateUS>=mdy(03, 04, 1893) & EndDateUS<mdy(03, 04, 1897)
replace Pres1 = "McKinley" if StartDateUS>=mdy(03, 04, 1897) & EndDateUS<mdy(09, 14, 1901)
replace Pres1 = "RooseveltT" if StartDateUS>=mdy(09, 14, 1901) & EndDateUS<mdy(03, 04, 1909)
replace Pres1 = "Taft" if StartDateUS>=mdy(03, 04, 1909) & EndDateUS<mdy(03, 04, 1913)
replace Pres1 = "Wilson" if StartDateUS>=mdy(03, 04, 1913) & EndDateUS<mdy(03, 04, 1921)
replace Pres1 = "Harding" if StartDateUS>=mdy(03, 04, 1921) & EndDateUS<mdy(08, 02, 1923)
replace Pres1 = "Coolidge" if StartDateUS>=mdy(08, 02, 1923) & EndDateUS<mdy(03, 04, 1929)
replace Pres1 = "Hoover" if StartDateUS>=mdy(03, 04, 1929) & EndDateUS<mdy(03, 04, 1933)
replace Pres1 = "RooseveltFD" if StartDateUS>=mdy(03, 04, 1933) & EndDateUS<mdy(04, 12, 1945)
replace Pres1 = "Truman" if StartDateUS>=mdy(04, 12, 1945) & EndDateUS<mdy(01, 20, 1953)
replace Pres1 = "Eisenhower" if StartDateUS>=mdy(01, 20, 1953) & EndDateUS<mdy(01, 20, 1961)
replace Pres1 = "Kennedy" if StartDateUS>=mdy(01, 20, 1961) & EndDateUS<mdy(11, 22, 1963)
replace Pres1 = "JohnsonLB" if StartDateUS>=mdy(11, 22, 1963) & EndDateUS<mdy(01, 20, 1969)
replace Pres1 = "Nixon" if StartDateUS>=mdy(01, 20, 1969) & EndDateUS<mdy(08, 09, 1974)
replace Pres1 = "Ford" if StartDateUS>=mdy(08, 09, 1974) & EndDateUS<mdy(01, 20, 1977)
replace Pres1 = "Carter" if StartDateUS>=mdy(01, 20, 1977) & EndDateUS<mdy(01, 20, 1981)
replace Pres1 = "Reagan" if StartDateUS>=mdy(01, 20, 1981) & EndDateUS<mdy(01, 20, 1989)
replace Pres1 = "BushGHW" if StartDateUS>=mdy(01, 20, 1989) & EndDateUS<mdy(01, 20, 1993)
replace Pres1 = "Clinton" if StartDateUS>=mdy(01, 20, 1993) & EndDateUS<mdy(01, 20, 2001)
replace Pres1 = "BushGW" if StartDateUS>=mdy(01, 20, 2001) & EndDateUS<mdy(01, 20, 2009)
replace Pres1 = "Obama" if StartDateUS>=mdy(01, 20, 2009) & EndDateUS<mdy(03, 13, 2015)

** Note: Some MIDs spillover across Presidencies
generate spillover = 0
replace spillover = 1 if Pres1==""

** Assigning spillover MIDs to first President to experience MID
replace Pres1 = "Madison" if StartDateUS>=mdy(03, 04, 1809) & spillover==1
replace Pres1 = "Monroe"  if StartDateUS>=mdy(03, 04, 1817) & spillover==1
replace Pres1 = "AdamsJQ" if StartDateUS>=mdy(03, 04, 1825) & spillover==1
replace Pres1 = "Jackson" if StartDateUS>=mdy(03, 04, 1829) & spillover==1
replace Pres1 = "Van_Buren" if StartDateUS>=mdy(03, 04, 1837) & spillover==1
replace Pres1 = "HarrisonWH" if StartDateUS>=mdy(03, 04, 1841) & spillover==1
replace Pres1 = "Tyler" if StartDateUS>=mdy(04, 04, 1841) & spillover==1
replace Pres1 = "Polk" if StartDateUS>=mdy(03, 04, 1845) & spillover==1
replace Pres1 = "Taylor" if StartDateUS>=mdy(03, 04, 1849) & spillover==1
replace Pres1 = "Fillmore" if StartDateUS>=mdy(03, 09, 1850) & spillover==1
replace Pres1 = "Pierce" if StartDateUS>=mdy(03, 04, 1853) & spillover==1
replace Pres1 = "Buchanan" if StartDateUS>=mdy(03, 04, 1857) & spillover==1
replace Pres1 = "Lincoln" if StartDateUS>=mdy(03, 04, 1861) & spillover==1
replace Pres1 = "JohnsonA" if StartDateUS>=mdy(04, 15, 1865) & spillover==1
replace Pres1 = "Grant" if StartDateUS>=mdy(03, 04, 1869) & spillover==1
replace Pres1 = "Hayes" if StartDateUS>=mdy(03, 04, 1877) & spillover==1
replace Pres1 = "Garfield" if StartDateUS>=mdy(03, 04, 1881) & spillover==1
replace Pres1 = "Arthur" if StartDateUS>=mdy(09, 19, 1881) & spillover==1
replace Pres1 = "Cleveland1" if StartDateUS>=mdy(03, 04, 1885) & spillover==1
replace Pres1 = "HarrisonB" if StartDateUS>=mdy(03, 04, 1897) & spillover==1
replace Pres1 = "Cleveland2" if StartDateUS>=mdy(03, 04, 1893) & spillover==1
replace Pres1 = "McKinley" if StartDateUS>=mdy(03, 04, 1897) & spillover==1
replace Pres1 = "RooseveltT" if StartDateUS>=mdy(09, 14, 1901) & spillover==1
replace Pres1 = "Taft" if StartDateUS>=mdy(03, 04, 1909) & spillover==1
replace Pres1 = "Wilson" if StartDateUS>=mdy(03, 04, 1913) & spillover==1
replace Pres1 = "Harding" if StartDateUS>=mdy(03, 04, 1921) & spillover==1
replace Pres1 = "Coolidge" if StartDateUS>=mdy(08, 02, 1923) & spillover==1
replace Pres1 = "Hoover" if StartDateUS>=mdy(03, 04, 1929) & spillover==1
replace Pres1 = "RooseveltFD" if StartDateUS>=mdy(03, 04, 1933) & spillover==1
replace Pres1 = "Truman" if StartDateUS>=mdy(04, 12, 1945) & spillover==1
replace Pres1 = "Eisenhower" if StartDateUS>=mdy(01, 20, 1953) & spillover==1
replace Pres1 = "Kennedy" if StartDateUS>=mdy(01, 20, 1961) & spillover==1
replace Pres1 = "JohnsonLB" if StartDateUS>=mdy(11, 22, 1963) & spillover==1
replace Pres1 = "Nixon" if StartDateUS>=mdy(01, 20, 1969) & spillover==1
replace Pres1 = "Ford" if StartDateUS>=mdy(08, 09, 1974) & spillover==1
replace Pres1 = "Carter" if StartDateUS>=mdy(01, 20, 1977) & spillover==1
replace Pres1 = "Reagan" if StartDateUS>=mdy(01, 20, 1981) & spillover==1
replace Pres1 = "BushGHW" if StartDateUS>=mdy(01, 20, 1989) & spillover==1
replace Pres1 = "Clinton" if StartDateUS>=mdy(01, 20, 1993) & spillover==1
replace Pres1 = "BushGW" if StartDateUS>=mdy(01, 20, 2001) & spillover==1
replace Pres1 = "Obama" if StartDateUS>=mdy(01, 20, 2009) & spillover==1


** Variable for the name of the President who was the first to inherit the MID
gen Pres2 = ""
replace Pres2 = "Obama" if spillover==1 & EndDateUS<mdy(01, 20, 2013)
replace Pres2 = "BushGW" if spillover==1 & EndDateUS<mdy(01, 20, 2009)
replace Pres2 = "Clinton" if spillover==1 & EndDateUS<mdy(01, 20, 2001)
replace Pres2 = "BushGHW" if spillover==1 & EndDateUS<mdy(01, 20, 1993)
replace Pres2 = "Reagan" if spillover==1 & EndDateUS<mdy(01, 20, 1989)
replace Pres2 = "Carter" if spillover==1 & EndDateUS<mdy(01, 20, 1981)
replace Pres2 = "Ford" if spillover==1 & EndDateUS<mdy(01, 20, 1977)
replace Pres2 = "Nixon" if spillover==1 & EndDateUS<mdy(08, 09, 1974)
replace Pres2 = "JohnsonLB" if spillover==1 & EndDateUS<mdy(01, 20, 1969)
replace Pres2 = "Kennedy" if spillover==1 & EndDateUS<mdy(11, 22, 1963)
replace Pres2 = "Eisenhower" if spillover==1 & EndDateUS<mdy(01, 20, 1961)
replace Pres2 = "Truman" if spillover==1 & EndDateUS<mdy(01, 20, 1953)
replace Pres2 = "RooseveltFD" if spillover==1 & EndDateUS<mdy(04, 12, 1945)
replace Pres2 = "Hoover" if spillover==1 & EndDateUS<mdy(03, 04, 1933)
replace Pres2 = "Coolidge" if spillover==1 & EndDateUS<mdy(03, 04, 1929)
replace Pres2 = "Harding" if spillover==1 & EndDateUS<mdy(08, 02, 1923)
replace Pres2 = "Wilson" if spillover==1 & EndDateUS<mdy(03, 04, 1921)
replace Pres2 = "Taft" if spillover==1 & EndDateUS<mdy(03, 04, 1913)
replace Pres2 = "RooseveltT" if spillover==1 & EndDateUS<mdy(03, 04, 1909)
replace Pres2 = "McKinley" if spillover==1 & EndDateUS<mdy(09, 14, 1901)
replace Pres2 = "Cleveland2" if spillover==1 & EndDateUS<mdy(03, 04, 1897)
replace Pres2 = "HarrisonB" if spillover==1 & EndDateUS<mdy(03, 04, 1893)
replace Pres2 = "Cleveland1" if spillover==1 & EndDateUS<mdy(03, 04, 1889)
replace Pres2 = "Arthur" if spillover==1 & EndDateUS<mdy(03, 04, 1885)
replace Pres2 = "Garfield" if spillover==1 & EndDateUS<mdy(09, 19, 1881)
replace Pres2 = "Hayes" if spillover==1 & EndDateUS<mdy(03, 04, 1881)
replace Pres2 = "Grant" if spillover==1 & EndDateUS<mdy(03, 04, 1877)
replace Pres2 = "JohnsonA" if spillover==1 & EndDateUS<mdy(03, 04, 1869)
replace Pres2 = "Lincoln" if spillover==1 & EndDateUS<mdy(04, 15, 1865)
replace Pres2 = "Buchanan" if spillover==1& EndDateUS<mdy(03, 04, 1861)
replace Pres2 = "Pierce" if spillover==1 & EndDateUS<mdy(03, 04, 1857)
replace Pres2 = "Fillmore" if spillover==1 & EndDateUS<mdy(03, 04, 1853)
replace Pres2 = "Taylor" if spillover==1 & EndDateUS<mdy(03, 09, 1850)
replace Pres2 = "Polk" if spillover==1 & EndDateUS<mdy(03, 04, 1849)
replace Pres2 = "Tyler" if spillover==1 & EndDateUS<mdy(03, 04, 1845)
replace Pres2 = "HarrisonWH" if spillover==1 & EndDateUS<mdy(04, 04, 1841)
replace Pres2 = "Van_Buren" if spillover==1 & EndDateUS<mdy(03, 04, 1841)
replace Pres2 = "Jackson" if spillover==1 & EndDateUS<mdy(03, 04, 1837)
replace Pres2 = "Quincy_Adams" if spillover==1 & EndDateUS<mdy(03, 04, 1829)
replace Pres2 = "Monroe"  if spillover==1 & EndDateUS<mdy(03, 04, 1825)
replace Pres2 = "Madison" if spillover==1 & EndDateUS<mdy(03, 04, 1817)

** Merge with US presidents data using variable `varname'

gen varname=Pres1   
sort varname

** see presidentnames.xlsx for code for next section
replace varname="madison" if varname=="Madison"
replace varname="monroe" if varname=="Monroe"
replace varname="adams2" if varname=="AdamsJQ"
replace varname="jackson" if varname=="Jackson"
replace varname="van_buren" if varname=="Van_Buren"
replace varname="harrison1" if varname=="HarrisonWH"
replace varname="tyler" if varname=="Tyler"
replace varname="polk" if varname=="Polk"
replace varname="taylor" if varname=="Taylor"
replace varname="fillmore" if varname=="Fillmore"
replace varname="pierce" if varname=="Pierce"
replace varname="buchanan" if varname=="Buchanan"
replace varname="lincoln" if varname=="Lincoln"
replace varname="johnson1" if varname=="JohnsonA"
replace varname="grant" if varname=="Grant"
replace varname="hayes" if varname=="Hayes"
replace varname="garfield" if varname=="Garfield"
replace varname="arthur" if varname=="Arthur"
replace varname="cleveland" if varname=="Cleveland1"
replace varname="harrison2" if varname=="HarrisonB"
replace varname="cleveland2" if varname=="Cleveland2"
replace varname="mckinley" if varname=="McKinley"
replace varname="roosevelt1" if varname=="RooseveltT"
replace varname="taft" if varname=="Taft"
replace varname="wilson" if varname=="Wilson"
replace varname="harding" if varname=="Harding"
replace varname="coolidge" if varname=="Coolidge"
replace varname="hoover" if varname=="Hoover"
replace varname="roosevelt2" if varname=="RooseveltFD"
replace varname="truman" if varname=="Truman"
replace varname="eisenhower" if varname=="Eisenhower"
replace varname="kennedy" if varname=="Kennedy"
replace varname="johnson2" if varname=="JohnsonLB"
replace varname="nixon" if varname=="Nixon"
replace varname="ford" if varname=="Ford"
replace varname="carter" if varname=="Carter"
replace varname="reagan" if varname=="Reagan"
replace varname="bush1" if varname=="BushGHW"
replace varname="clinton" if varname=="Clinton"
replace varname="bush2" if varname=="BushGW"
replace varname="obama" if varname=="Obama"

sort varname
merge m:1 varname using "1-Original-Data/USPres2010-10-13.dta"                                         
tab _merge
order _merge
** Dropping Presidents not in the MID dataset
drop if _merge==2
drop _merge
sort StartDateUS dispnum3 dispnum4

** Counting number of bilateral disputes
*"those involving the United States and only one other country"
count if numa==1 & numb==1

** Counting number of disputes with the US as an originator
*"report the results for multiparty disputes in which the United States was an originator to the dispute"
count if orig==1

save "3-Analysis-Data/15-03-13-merging.dta", replace

** The following do files finish the merging process, each producing a separate dataset with a different coding of Southernness

**Main coding, Truman not Southern, GWBush Southern
do "2-150317-Data-Merge/2-150317-data-merge1.do"
*Produces "15-03-13-USMIDs1.dta"

**Truman Southern, GWBush Southern
do "2-150317-Data-Merge/2-150317-data-merge2.do"
*Produces "15-03-13-USMIDs2.dta"

**Truman not Southern, GWBush not Southern
do "2-150317-Data-Merge/2-150317-data-merge3.do"
*Produces "15-03-13-USMIDs3.dta"

**Truman Southern, GWBush not Southern
do "2-150317-Data-Merge/2-150317-data-merge4.do"
*Produces "15-03-13-USMIDs4.dta"

log close
